package com.example.demo.demo;

import com.example.demo.model.LoginInfo;
import com.example.demo.model.Student;
import org.junit.Test;

import java.util.*;

/**
 * java 值传递测试
 *
 * java中无论是引用类型还是基本类型，在参数传递时都是值传递
 *
 * @author luowj12
 * @since 2021/6/1 15:25
 */
public class YingyongTest {


    public static void main(String[] args){
        //A();
        //B();
        //C();
        //D();
        //E();

    }
    @Test
    public void  E(){
        Student s =new Student("11");
        String str = "2";
        s.setName(str);
        LoginInfo login = new LoginInfo("名字");
        LoginInfo login1 = new LoginInfo("名字修改了");
        s.setLoginInfo(login);
        login.setUsername("修改了");
        login = login1;
        change2(s);
        str = "修改了字符串";
        System.out.println(s.getLoginInfo().getUsername()+s.getName());
    }


    @Test
    public  void C(){
        Student s =new Student("11");
        change(s);
    }

    /**
     * 不会改变
     * @param student
     * @return
     */
    public  static Student change2(Student student){
        Student s1 =new Student("修改了333");
        student = s1;
        return student;
    }

    /**
     * 会改变
     * @param s
     */
    public static void change(Student s){
        //s.setName("修改了");
        s = new Student("修改了");
    }

    @Test
    public void A(){
        //测试引用传递
        Map<String,String> a = new HashMap<>();
        a.put("1", "a");
        Map<String,String> a1 = new HashMap<>();
        a1.put("1", "修改了a1");
        List<Map> b = new ArrayList<Map>();
        b.add(a);
        //a = null;
        a=a1;
        //a.put("1","修改了");
        Map aa = b.get(0);
        System.out.println(aa.get("1"));
        //结论传递的是引用的副本
    }

    @Test
    public  void B(){
        //测试值传递
        int xx = 1;
        add(xx);
        System.out.println(xx);
    }


    @Test
    public  void D(){

        List list1 = new ArrayList();
        list1.add(2);
        List list2 = list1;
        list1.add(5);
        System.out.println(list2.size());

    }


    public static void add(int temp) {
        temp++;
    }

    @Test
    public void  F(){
        LoginInfo loginOld = new LoginInfo("名字");
        LoginInfo loginNew = loginOld;
        loginNew.setUsername("修改了");
        System.out.println(loginOld.getUsername());
    }

    /**
     * 不会改变
     */
    @Test
    public void G(){
        LoginInfo login = new LoginInfo();
        String name = login.getUsername();
        login.setUsername("修改了");
        System.out.println(name);
    }
}
